package com.credithc.customers.base.utils;

import lombok.Synchronized;

import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Company: Credithc
 * User: Dapeng Wang
 * Date: apple
 * Time: 下午4:38
 */
public class RandomUtils {
    private static Random random = new Random();

    /**
     * 生成随机数字符串
     *
     * @param length 随机数长度
     * @return
     */
    @Synchronized
    public static String randomString(int length) {
        String tmp = System.currentTimeMillis() + "" + Math.abs(random.nextInt());
        while (tmp.length() < length) {
            tmp = System.currentTimeMillis() + tmp;
        }
        if (tmp.length() > length) {
            tmp = tmp.substring(tmp.length() - length, tmp.length());
        }
        return tmp;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(RandomUtils.randomString(6));
//            System.out.println(RandomStringUtils.randomNumeric(6) + String.valueOf(System.currentTimeMillis()));
        }
    }

// 是否重复测试
//    public static void main(String[] args){
//        ExecutorService executorService = Executors.newFixedThreadPool(20);
//        for (int i = 0; i < 500; i ++) {
//        executorService.execute(new MyThread());}
//    }
//    public static class MyThread extends Thread{
//        @Override
//        public void run() {
//            System.out.println(RandomStringUtils.randomNumeric(6) + String.valueOf(System.currentTimeMillis()));
//        }
//    }

}
